package xj.graph2d.app.project;

import static xj.app.BaseApplicationFrame.getResourceString;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.text.MessageFormat;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import xj.graph2d.DocumentException;
import xj.graph2d.app.DrawAction;
import xj.graph2d.app.DrawApp;
import xj.graph2d.dialog.DialogPanel;
import xj.graph2d.dialog.TitledDialogPanel;

/**
 *  Rename a project item or folder 
 */
public class ProjectFolderPropertiesListener 
  extends DrawAction 
  implements ActionListener {

  class ProjectFolderOptionPanel extends DialogPanel {

    public ProjectFolderOptionPanel(String name) {
      super(name);
      nameLabel = new JLabel(getResourceString("ProjectItemName.text") + " ");
      nameField = new JTextField();

      JLabel descriptionLabel = 
	new JLabel(getResourceString("Description.text") + " ");
      descriptionLabel.setVerticalAlignment(SwingConstants.TOP);
      descriptionField = new JTextArea(4, 40);
      descriptionField.setBorder(BorderFactory.createEtchedBorder());

      addRow(nameLabel, nameField);
      addRow(descriptionLabel, new JScrollPane(descriptionField));

      setPreferredSize(new Dimension(400, 250));
      setOption();
    }

    @Override
    public void setOption() {
      if (item != null) {
	String name = item.getName();
	String des = item.getDescription();
	nameField.setText(name);
	descriptionField.setText(des);
      }
    }

    @Override
    public void applyOption() {
      if (item != null) {
	boolean exists = false; 
	String name = nameField.getText();
	String des = descriptionField.getText();
	item.setDescription(des);

	if (name != null && 
	    !name.equals(item.getName())) {
	  ProjectFolder parent = item.getParent();
	  if (parent != null) { 
	    String path = parent.getFullPath();
	    String filename = path + File.separator + name;
	    File file = new File(filename);
	    exists = file.exists();
	  }
	  
	  if (exists) { 
	    String title, message;
	    title = getResourceString("MoveFolder.Error.title");
	    message = getResourceString("MoveFolder.Error.message");
	    message = MessageFormat.format(message, name, parent.getName());
	    JOptionPane.showMessageDialog(main.getJFrame(), 
					  message, title, 
					  JOptionPane.ERROR_MESSAGE);
	  } else { 
	    String oldfilename = item.getDocumentFullPath();
	    File oldfile = new File(oldfilename);
	    oldfile.delete();

	    item.setName(name);
	    Project project = item.getOwner();
	    ProjectListener listener = main.getProjectListener();
	    listener.projectItemRenamed(item);

	    try { 
	      project.saveProject();
	    } catch (IOException e) {
	      System.err.println(e); 
	    } catch (DocumentException e) { 
	      System.err.println(e);
	    }
	  }
	}
      }
    }

    protected JTextField nameField;
    protected JLabel     nameLabel;

    protected JTextArea descriptionField;

  }

  protected TitledDialogPanel projectFolderOptionDialog = null;
  protected ProjectFolderOptionPanel projectFolderPanel = null;

  //protected String projectFolderPanelTitle;
  protected String projectFolderDialogTitle;
  protected String projectFolderHeader;
  protected String[] projectFolderText;
  protected String projectFolderName;
  protected Icon projectFolderIcon;

  protected ProjectFolder item;

  @Override
protected void init() { 
    //projectFolderPanelTitle = getResourceString("ProjectFolder.title");
    projectFolderDialogTitle = getResourceString("ProjectFolderDialog.title");
    projectFolderName = getResourceString("ProjectFolderName.text");
    projectFolderHeader = getResourceString("ProjectFolderDialog.header");
    projectFolderText = new String[] { 
	getResourceString("ProjectFolderDialog.text1"),
	getResourceString("ProjectFolderDialog.text2"),	
    };
    projectFolderIcon = DrawApp.getIcon("NewFolderWizIcon");
  }

  public void setProjectFolder(ProjectFolder item) {
    this.item = item;
  }

  public void actionPerformed(ActionEvent e) {
    if (projectFolderOptionDialog == null) {
      DialogPanel[] options = { 
	projectFolderPanel = new ProjectFolderOptionPanel(null), //projectItemPanelTitle),
      };

      projectFolderOptionDialog = new TitledDialogPanel(main.getJFrame(),  
							JOptionPane.OK_CANCEL_OPTION, 
							null, options);
    }


    projectFolderPanel.nameLabel.setText(projectFolderName);
    projectFolderOptionDialog.setTitle(projectFolderDialogTitle);
    projectFolderOptionDialog.setTitlePaneText(projectFolderHeader, projectFolderText,
					       projectFolderIcon);

    projectFolderOptionDialog.setOptions();
    projectFolderOptionDialog.showOptionDialog();
  }

}
